<%@page import="net.sf.json.JSONArray"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="info.teacher"%>
<%@page import="info.test"%>
<%@page import="info.student"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="edit_test.js"></script>
<%
	
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
	int id=Integer.parseInt(request.getParameter("id"));
	Connection con=(Connection) application.getAttribute("con");
	Statement sm=con.createStatement();
	String sqls1="SELECT * FROM test WHERE id="+id;
	ResultSet rs=sm.executeQuery(sqls1);
	rs.last();
	int len=rs.getRow();
	rs.beforeFirst();
	if(len!=1){
%>
<script type="text/javascript">
alert("错误，没有查到记录，系统将返回！");
window.history.go(-1);
</script>
<%
	return ;
	}
	rs.next();
	test tt=new test();
	tt.id=rs.getInt(1);
	tt.caption=rs.getString(2);
	tt.state=rs.getInt(3);
	tt.user=rs.getString(4);
	tt.time=rs.getInt(5);
	tt.teacher=rs.getString(6);
	tt.student=rs.getString(7);
	String sqls2="SELECT * FROM teacher";
	rs=sm.executeQuery(sqls2);
	rs.last();
	int len2=rs.getRow();
	rs.beforeFirst();
	ArrayList<teacher> alt=null;
	if(len2!=0){
		alt=new ArrayList<teacher>();
		for(int i=0;i<len2;++i){
			teacher tct=new teacher();
			rs.next();
			tct.id=rs.getInt(1);
			tct.name=rs.getString(2);
			tct.test=rs.getString(3);
			alt.add(tct);
		}
	}
	String sqls3="SELECT * FROM student";
	rs=sm.executeQuery(sqls3);
	rs.last();
	int len3=rs.getRow();
	rs.beforeFirst();
	ArrayList<student> als=null;
	if(len3!=0){
		als=new ArrayList<student>();
		for(int i=0;i<len3;++i){
			student tct=new student();
			rs.next();
			tct.id=rs.getInt(1);
			tct.name=rs.getString(2);
			tct.test=rs.getString(3);
			als.add(tct);
		}
	}
%>
<title>Insert title here</title>
<script type="text/javascript">
var lent=<%=len2 %>
var lens=<%=len3 %>
var id=<%=id %>
</script>
</head>
<body>
<center>
<h3><font color="red">编辑考试</font></h3>
<br/>
<table border="1">
	<tr>
		<td>序号：</td>
		<td><%=tt.id %></td>
	</tr>
	<tr>
		<td>标题：</td>
		<td><input type="text" id="caption" name="caption" style="width: 400px;" value="<%=tt.caption %>" /></td>
	</tr>
	<tr>
		<td>状态：</td>
		<td>
			<select id="state" name="state">
				<option value="0" <%out.print(tt.state==0?"selected=\"selected\"":""); %>>未开始</option>
				<option value="1" <%out.print(tt.state==1?"selected=\"selected\"":""); %>>已开始</option>
				<option value="2" <%out.print(tt.state==2?"selected=\"selected\"":""); %>>在判题</option>
				<option value="3" <%out.print(tt.state==3?"selected=\"selected\"":""); %>>已结束</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>用户：</td>
		<td><%=tt.user %></td>
	</tr>
	<tr>
		<td>考试时间（秒）：</td>
		<td><input type="text" id="time" name="time" style="width: 400px;" value="<%=tt.time %>" /></td>
	</tr>
	<tr>
		<td>参与的教师：</td>
		<td>
			<button id="edit_teacher_button" onclick="edit_teacher()">展开编辑</button>
			<div id="edit_teacher" style="word-break:break-all; width:400px; display: none;">
				<!-- 强制换行 -->
<%
	String ats="var at=new Array(";
	boolean flagoffirst=true;
	if(tt.teacher.equals(null)==true || tt.teacher.equals("")==true){
		if(len2==0){
			out.println("<font color=\"red\">没有教师用户，您可以进行添加！</font>");
		}else{
			Iterator<teacher> iter=alt.iterator();
			while(iter.hasNext()==true){
				teacher tct=iter.next();
				out.println("<input type=\"checkbox\" id=\"t"+tct.id+"\" name=\"t"+tct.id+"\" />"+tct.name+"("+tct.id+")");
				if(flagoffirst==true){
					ats+=tct.id;
					flagoffirst=false;
				}else{
					ats+=","+tct.id;
				}
			}
		}
	}else{
		JSONObject jo=JSONObject.fromObject(tt.teacher);
		JSONArray ja=jo.getJSONArray("teacher");
		int lenj1=ja.size();
		if(len2==0){
			out.println("<font color=\"red\">没有教师用户，您可以进行添加！</font>");
		}else{
			Iterator<teacher> iter=alt.iterator();
			while(iter.hasNext()==true){
				teacher tct=iter.next();
				boolean flag=false;
				for(int i=0;i<lenj1;++i){
					JSONObject jooi=ja.getJSONObject(i);
					String jagi=jooi.getString("name");
					if(tct.name.equals(jagi)==true){
						flag=true;
						out.println("<input type=\"checkbox\" id=\"t"+tct.id+"\" name=\"t"+tct.id+"\" checked=\"checked\" />"+tct.name+"("+tct.id+")");
						if(flagoffirst==true){
							ats+=tct.id;
							flagoffirst=false;
						}else{
							ats+=","+tct.id;
						}
						break;
					}
				}
				if(flag==false){
					out.println("<input type=\"checkbox\" id=\"t"+tct.id+"\" name=\"t"+tct.id+"\" />"+tct.name+"("+tct.id+")");
					if(flagoffirst==true){
						ats+=tct.id;
						flagoffirst=false;
					}else{
						ats+=","+tct.id;
					}
				}
			}
		}
	}
	ats+=");";
%>
<script type="text/javascript">
<%=ats %>
</script>
			</div> 
		</td>
	</tr>
	<tr>
		<td>参与的考生：</td>
		<td>
			<button id="edit_student_button" onclick="edit_student()">展开编辑</button>
			<div id="edit_student" style="word-break:break-all; width:400px; display: none;">
				<!-- 强制换行 -->
<%
	flagoffirst=true;
	String ass="var as=new Array(";
	if(tt.student.equals(null)==true || tt.student.equals("")==true){
		if(len3==0){
			out.println("<font color=\"red\">没有考生用户，您可以进行添加！</font>");
		}else{
			Iterator<student> iter=als.iterator();
			while(iter.hasNext()==true){
				student tct=iter.next();
				out.println("<input type=\"checkbox\" id=\"s"+tct.id+"\" name=\"s"+tct.id+"\" />"+tct.name+"("+tct.id+")");
				if(flagoffirst==true){
					ass+=tct.id;
					flagoffirst=false;
				}else{
					ass+=","+tct.id;
				}
			}
		}
	}else{
		JSONObject jo=JSONObject.fromObject(tt.student);
		JSONArray ja=jo.getJSONArray("student");
		int lenj2=ja.size();
		if(len3==0){
			out.println("<font color=\"red\">没有考生用户，您可以进行添加！</font>");
		}else{
			Iterator<student> iter=als.iterator();
			while(iter.hasNext()==true){
				student tct=iter.next();
				boolean flag=false;
				for(int i=0;i<lenj2;++i){
					JSONObject jooi=ja.getJSONObject(i);
					String jagi=jooi.getString("name");
					if(tct.name.equals(jagi)==true){
						flag=true;
						out.println("<input type=\"checkbox\" id=\"s"+tct.id+"\" name=\"s"+tct.id+"\" checked=\"checked\" />"+tct.name+"("+tct.id+")");
						if(flagoffirst==true){
							ass+=tct.id;
							flagoffirst=false;
						}else{
							ass+=","+tct.id;
						}
						break;
					}
				}
				if(flag==false){
					out.println("<input type=\"checkbox\" id=\"s"+tct.id+"\" name=\"s"+tct.id+"\" />"+tct.name+"("+tct.id+")");
					if(flagoffirst==true){
						ass+=tct.id;
						flagoffirst=false;
					}else{
						ass+=","+tct.id;
					}
				}
			}
		}
	}
	ass+=");";
%>
<script type="text/javascript">
<%=ass %>
</script>
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<center>
				<button onclick="commit()">提交</button>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<button onclick="go_back()">返回</button>
			</center>
		</td>
	</tr>
</table>
</center>
</body>
</html>